package com.ambitor.ipro.actions;

import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.sql.dao.impl.SuperDaoImpl;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.ambitor.ipro.util.Constants;
import com.ambitor.ipro.util.DateUtil;

public class FeedbackAction extends BaseAction {

	private Map<String, String> feedback;

	private IDataset feedbacks;

	SuperDao superDao = null;

	private static final long serialVersionUID = -6806405160322774348L;

	public FeedbackAction() throws SQLException {
		superDao = new SuperDaoImpl(pd.getConn());
	}

	public IDataset getFeedbacks() {
		if (feedbacks == null)
			return new DatasetList();
		return feedbacks;
	}

	public void setFeedbacks(IDataset feedbacks) {
		this.feedbacks = feedbacks;
	}

	public Map<String, String> getFeedback() {
		return feedback;
	}

	public void setFeedback(Map<String, String> feedback) {
		this.feedback = feedback;
	}

	@SuppressWarnings("unchecked")
	public String addFeedback() throws Exception {
		IData feedabckMap = new DataMap(this.getFeedback());
		feedabckMap.put("FEEDBACK_TIME", pd.getTime());
		feedabckMap.put("NICK", pd.getTopNick());
		feedabckMap.put("IS_REPLY", Constants.FLAG_NO);

		superDao.save("TF_P_FEEDBACK", feedabckMap);
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String queryMyFeedbacks() {
		String nick = pd.getTopNick();
		IData param = new DataMap();
		param.put("NICK", nick);
		String sql = "select * from TF_P_FEEDBACK where nick=:NICK"
				+ " order by feedback_id desc";
		try {
			feedbacks = superDao.queryListBySql(sql, param);
		} catch (Exception e) {
			logger.warn("", e);
		}
		return "my_feedback";
	}

	@SuppressWarnings("unchecked")
	public String queryAllUnreplyFeedbacks() {
		IData param = new DataMap();
		String sql = "select * from TF_P_FEEDBACK where is_reply = 0";
		try {
			feedbacks = superDao.queryListBySql(sql, param);
		} catch (Exception e) {
			logger.warn("", e);
		}
		return "reply_feedback";
	}

	public String replyFeedback() {
		feedback.put("IS_REPLY", Constants.FLAG_YES);
		feedback.put("REPLY_TIME", DateUtil.formatDate(new Date()));
		feedback.put("REPLY_PERSON", pd.getTopNick());
		try {
			superDao.update("TF_P_FEEDBACK", new DataMap(feedback));
		} catch (Exception e) {
			logger.warn("保存回复失败");
		}
		return "success";
	}
}
